#! /usr/bin/env bash
. $HOME/.envy/.rcfunctions

# --------------------------------------------------------------------------- 
bash_setup() {
    if [[ ! -f $HOME/bin/envy ]]; then
        ln -s $HOME/.envy/envy $HOME/bin/envy
    fi
    patch .bash_profile
    patch .bashrc
    patch .aliases
    patch .environment
}

# --------------------------------------------------------------------------- 
patch()
{
    if [[ -f $HOME/$1 ]]; then
        if [[ ! -f $HOME/${1}.original ]]; then
            mv $HOME/$1 $HOME/${1}.original
        fi
    fi
    if [[ ! -f $1 ]]; then
        ln -s .envy/$1 $HOME/$1
    fi
}

# --------------------------------------------------------------------------- 
fetch() 
{
    scp $2 $HOME/tmp/envy_fetched.tar.gz
    unpack $HOME/tmp/envy_fetched.tar.gz
}

# --------------------------------------------------------------------------- 
help() 
{
    case "$1" in
        help)
           cat - <<EOF
envy v $VERSION

   The help command provides information about envy's various functions.

   Usage: envy help [<function>]

   Examples:
      envy help help
      envy help setup

EOF
        ;;

        pack)
           cat - <<EOF
envy v $VERSION

   Pack the current environment framework up for deployment elsewhere.

   Usage: envy pack

   Pack up the current environment framework in \$HOME/tmp/envy.tar.gz. 
   If the gzip utility is not available, the tar file will be left
   uncompressed. This tar file can be unpacked into another account to
   deploy envy (with your settings) there.

EOF
        ;;

        remove)
           cat - <<EOF
envy v $VERSION

   Remove the links from \$HOME into \$HOME/.envy.

   Usage: envy remove

   This will remove the symlinks from \$HOME that point into \$HOME/.envy.

EOF
        ;;

        setup)
           cat - <<EOF
envy v $VERSION

   Create links from \$HOME to the files in \$HOME/.envy

   Usage: envy setup

   This function will create symlinks in \$HOME to point at .bash_profile,
   .bashrc, .aliases, .environment, and .rcfunctions in \$HOME/.envy.

EOF
        ;;

        unpack)
           cat - <<EOF
envy v $VERSION

   Unpack \$HOME/tmp/envy.tar.gz into \$HOME/.envy

   Usage: envy pack [<tarball-path>]

   By default, unpack \$HOME/tmp/envy.tar.gz. If a tarball path is
   indicated on the command line, it will be used.

EOF
        ;;

        wipe)
           cat - <<EOF
envy v $VERSION

   Completely remove envy from the current account.

   Usage: envy wipe

   Symlinks are removed from \$HOME, then \$HOME/.envy is removed.
   Obviously, be very sure before you run this. Unless you've stashed
   an envy tarball somewhere to unpack into \$HOME/.envy, you won't be
   able to recover.
   
EOF
        ;;

        *)
           cat - <<EOF
envy v $VERSION

   help         this message
   pack         pack up envy for transfer elsewhere
   remove       remove the links into envy
   setup        setup links into envy
   unpack       unpack a tarball into the .envy directory
   wipe         completely remove envy from this account

What is envy?

   envy is a simple environment manager that can be quickly unpacked
   into a Unix account to create a framework for setting and managing
   one's personal environment.

   If you're reading this, envy has been deployed for the account you
   are using. What this means is that the files controlling the
   environment (.bashrc, .bash_profile, etc.) live in $HOME/.envy. In
   $HOME, there are symlinks with the names expected by bash pointing
   at these files. To begin, envy provides the following files:

      .aliases - handy command aliases; called from .bashrc
      .bash_profile - the profile bash runs at login
      .bashrc - bash runs this each time it's started, even if it's 
         not a login session
      .environment - place to set environment variables; called from 
         .bashrc

   You can add settings to the files above or even add files if you want.
   To add a file, just put it in \$HOME/.envy. The files above can reference
   it there, or if need be, you can create a symlink to your new file from
   $HOME.

      $ ln -s \$HOME/.envy/<filename> \$HOME/<filename>

   For more information about the envy subfunctions,

      $ envy help <function>

EOF
        ;;
    esac
}

# --------------------------------------------------------------------------- 
ksh_setup() 
{
   ln -s .envy/.bash_profile $HOME/.profile
   ln -s .envy/.bashrc $HOME/.kshrc
   ln -s .envy/envy $HOME/bin/envy
}

# --------------------------------------------------------------------------- 
pack() 
{
   cd $HOME
   TS=`date +%Y.%m%d`
   $TAR cvf $HOME/envy-${TS}.tar ./.envy
   if [ "$GZIP" != "" ]; then
      $GZIP $HOME/tmp/envy.tar
   else
      echo "Can't compress $HOME/tmp/envy.tar -- no gzip available."
   fi
}

# --------------------------------------------------------------------------- 
remove() 
{
   FILELIST="$HOME/.bash_profile 
             $HOME/.bashrc 
             $HOME/.profile 
             $HOME/.kshrc 
             $HOME/.aliases
             $HOME/.aliases.original
             $HOME/.environment
             $HOME/.environment.original
             $HOME/bin/envy"

   for file in $FILELIST
   do
      if [ -L $file ]; then
         rm $file
      fi
   done

   unpatch .bash_profile
   unpatch .bashrc
}

# --------------------------------------------------------------------------- 
unpatch()
{
    if [[ -f ${1}.original ]]; then
        mv ${1}.original $1
    fi
}
# --------------------------------------------------------------------------- 
set_tar() 
{
    TAR=`which tar`
    GTAR=`which gtar`
    if [ -x "$GTAR" ]; then
        TAR=$GTAR
    fi

    GZIP=`which gzip`
    if [ $? != 0 ]; then
       GZIP=""
    fi
}

# --------------------------------------------------------------------------- 
wipe() 
{
    cd $HOME
    remove
    rm -rf .envy
}

# --------------------------------------------------------------------------- 
setup() 
{
   ln -s $HOME/.envy/envy $HOME/bin/envy
   case $SHELL in
      /usr/bin/ksh)
         ksh_setup
      ;;

      /bin/ksh)
         ksh_setup
      ;;

      /usr/local/gnu/bin/bash)
         bash_setup
      ;;

      /usr/bin/bash)
         bash_setup
      ;;

      /bin/bash)
         bash_setup
      ;;

      *)
         echo "no case for $SHELL"
      ;;
   esac
}

# --------------------------------------------------------------------------- 
unpack() 
{
   cd $HOME/.envy
   $TAR zxvf ${1:-$HOME/tmp/envy.tar.gz}
}

# --------------------------------------------------------------------------- 
# "main"
set_tar
case $1 in
   distribute)
      distribute
   ;;

   help)
      help $2
   ;;

   pack)
      pack
   ;;

   push)
      push $1 $2
   ;;

   remove)
      remove
   ;;

   setup)
      setup
   ;;

   unpack)
      unpack
   ;;

   wipe)
      wipe
   ;;

   *)
      echo "unknown request: $1"
   ;;
esac
